package com.a2v2.online.store.service.impl;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;

import com.a2v2.online.store.common.constant.NotificationConstants;
import com.a2v2.online.store.helper.NotificationServiceHelper;
import com.a2v2.online.store.service.constant.EmailProperties;
import com.a2v2.online.store.service.contract.INotificationService;
import com.a2v2.online.store.service.dto.NotificationDTO;
import com.a2v2.online.store.service.dto.NotificationDetailDTO;

public class NotificationServiceImpl implements INotificationService {

	@Autowired
	private EmailProperties emailProperties;
	
	@Override	
	public void sendNotification(NotificationDTO notificationDTO) {
		/**
		 * 1. We need to check whether the account is blocked for notification or not.
		 * 2. If not, then check the notification channel whether it is EMAIL or SMS.
		 *  
		 */
		if(!NotificationServiceHelper.isUserAccountLocked(notificationDTO))
		{
			if(NotificationConstants.SEND_MAIL_NOTIFICATION.equalsIgnoreCase(notificationDTO.getChannel().getChannel()))
			{
				sendMailNotification(notificationDTO);
			}
			else if (NotificationConstants.SEND_SMS_NOTIFICATION.equalsIgnoreCase(notificationDTO.getChannel().getChannel())) {
				sendSMSNotification(notificationDTO);
			}
		}
	}

	private void sendMailNotification(NotificationDTO notificationDTO)
	{
		/**
		 * 1. we need to get the template first
		 * 2. Load the template
		 * 3. construct the mail template
		 * 4. make an entry into NOTIFICATION_ACTION and NOTIFICATION_STATUS table.
		 * 5. send mail.
		 */
		NotificationDetailDTO notificationDetailDTO = NotificationServiceHelper.getMessageTemplate(notificationDTO);
		if(notificationDetailDTO.getTemplateKey()!=null) //Template found now load the message
		{
			notificationDetailDTO.setNotificationChannel(notificationDTO.getChannel());
			notificationDetailDTO.setReceipientMailAddr(notificationDTO.getCustomerEmail());
			notificationDetailDTO = NotificationServiceHelper.loadMessage(notificationDetailDTO);
			// TODO: We need to set the subjectParamMap and messageParamMap
			// for subjectParamMap we need to get the data from PI or somewhere.
			// for messageParamMap we are already getting the data from notificationDTO.
			notificationDetailDTO.setMessageParamMap(notificationDTO.getParamMap());
			notificationDetailDTO.setSubjectParamMap(notificationDTO.getParamMap());
			MimeMessage mimeMessage = NotificationServiceHelper.constructMailTemplate(notificationDetailDTO);
			//TODO: Need to make an entry in NOTIFICATION_ACTION and NOTIFICATION_STATUS table.
			sendMail(mimeMessage);
		}
		
	}
	private void sendSMSNotification(NotificationDTO notificationDTO)
	{
		
	}
	
	private void sendMail(MimeMessage mimeMessage)
	{
		try
		{
			Properties prop = emailProperties.getProperties();
			Session session = Session.getDefaultInstance(prop);
			Transport transport = session.getTransport(prop.getProperty("mail.transport.protocol"));
			transport.connect(prop.getProperty("mail.smtp.host"), prop.getProperty("mail.username"), prop.getProperty("mail.password"));
			transport.sendMessage(mimeMessage, mimeMessage.getRecipients(Message.RecipientType.TO));
			transport.close();
			System.out.println("************ MAIL SENT ********************");
		}
		catch(Exception ex)
		{
			//TODO : need to implement this.
			ex.printStackTrace();
		}
	}
}
